//package com.example.demo.config.handler;
//
//import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
//import com.quartz.demo.annotation.SensitiveData;
//import com.quartz.demo.service.DecryptService;
//import org.apache.ibatis.executor.resultset.ResultSetHandler;
//import org.apache.ibatis.plugin.Interceptor;
//import org.apache.ibatis.plugin.Intercepts;
//import org.apache.ibatis.plugin.Invocation;
//import org.apache.ibatis.plugin.Signature;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.annotation.AnnotationUtils;
//import org.springframework.stereotype.Component;
//
//import java.sql.Statement;
//import java.util.ArrayList;
//import java.util.Objects;
//import java.util.Properties;
//
//@Component
//@Intercepts(
//    @Signature(type = ResultSetHandler.class,method = "handleResultSets",args = {Statement.class})
//)
//public class DecryptInterceptor implements Interceptor {
//
//  private DecryptService decryptService;
//
//  @Autowired
//  public DecryptInterceptor(DecryptService decryptService) {
//    this.decryptService = decryptService;
//  }
//
//  @Override
//  public Object intercept(Invocation invocation) throws Throwable {
//    //取出查询的结果
//    Object resultObject = invocation.proceed();
//    if (Objects.isNull(resultObject)) {
//      return null;
//    }
//    //基于selectList
//    if (resultObject instanceof ArrayList) {
//      ArrayList resultList = (ArrayList) resultObject;
//      if (!CollectionUtils.isEmpty(resultList) && needToDecrypt(resultList.get(0))) {
//        for (Object result : resultList) {
//          //逐一解密
//          decryptService.decrypt(result);
//        }
//      }
//      //基于selectOne
//    } else {
//      if (needToDecrypt(resultObject)) {
//        decryptService.decrypt(resultObject);
//      }
//    }
//    return resultObject;
//  }
//
//  private boolean needToDecrypt(Object object) {
//    Class<?> objectClass = object.getClass();
//    SensitiveData sensitiveData = AnnotationUtils.findAnnotation(objectClass, SensitiveData.class);
//    return Objects.nonNull(sensitiveData);
//  }
//  @Override
//  public Object plugin(Object target) {
//    return Interceptor.super.plugin(target);
//  }
//
//  @Override
//  public void setProperties(Properties properties) {
//    Interceptor.super.setProperties(properties);
//  }
//}